<template>
  <div class="dish-list">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>菜品列表</span>
              <el-button type="primary" @click="showAddDialog">添加菜品</el-button>
            </div>
          </template>
          
          <el-table :data="dishes" style="width: 100%">
            <el-table-column prop="id" label="ID" width="80" />
            <el-table-column prop="name" label="菜品名称" />
            <el-table-column prop="price" label="价格" />
            <el-table-column prop="description" label="描述" />
            <el-table-column label="操作" width="200">
              <template #default="scope">
                <el-button size="small" @click="showEditDialog(scope.row)">编辑</el-button>
                <el-button size="small" type="danger" @click="deleteDish(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <!-- Add/Edit Dialog -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEdit ? '编辑菜品' : '添加菜品'"
      width="30%"
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="菜品名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="价格">
          <el-input-number v-model="form.price" :min="0" :precision="2" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveDish">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { dishApi } from '../api'

// State
const dishes = ref([])
const dialogVisible = ref(false)
const isEdit = ref(false)
const form = ref({
  name: '',
  price: 0,
  description: ''
})

// Methods
const loadDishes = async () => {
  try {
    const response = await dishApi.getDishes()
    dishes.value = response.data
  } catch (error) {
    ElMessage.error('加载菜品列表失败')
  }
}

const showAddDialog = () => {
  isEdit.value = false
  form.value = {
    name: '',
    price: 0,
    description: ''
  }
  dialogVisible.value = true
}

const showEditDialog = (dish) => {
  isEdit.value = true
  form.value = { ...dish }
  dialogVisible.value = true
}

const saveDish = async () => {
  try {
    if (isEdit.value) {
      await dishApi.updateDish(form.value.id, form.value)
      ElMessage.success('更新成功')
    } else {
      await dishApi.addDish(form.value)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    loadDishes()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

const deleteDish = async (id) => {
  try {
    await ElMessageBox.confirm('确定要删除这个菜品吗？', '提示', {
      type: 'warning'
    })
    await dishApi.deleteDish(id)
    ElMessage.success('删除成功')
    loadDishes()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// Lifecycle
onMounted(() => {
  loadDishes()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 